<template>
  <div></div>
</template>

<script>
  
import md from '@/markdowns/algor-lengthOfLongestSubstring.md'

export default {
  name: 'algor-lengthOfLongestSubstring',
  components: {
    
  },
  data () {
    return {
      
    }
  },
  watch: {
    
  },
  computed: {
    
  },
  methods: {
    init () {
      const getIndexFromSubStr = (str, head, tail, ch) => {
          for (let i = head; i < tail; i++) {
              if (ch === str[i]) {
                  return i
              }
          }
          return -1
      }
      
      /**
       * @param {string} s
       * @return {number}
       */
      var lengthOfLongestSubstring = function(s) {
          console.log(s)
          const len = s.length;
          let head = 0
          let tail = 0
          let res = 0
          for(let i = 0; i < len; i ++) {
              const index = getIndexFromSubStr(s, head, tail, s[i])
              console.log(i, head, tail, index)
              if (index >= 0) {
                  head = index + 1
              }
              tail++
              const resLen = tail - head
              if (resLen > res) {
                  res = resLen
              }
          }
          return res;
      };
      
      console.log(lengthOfLongestSubstring('abcabcabb'))
      console.log(lengthOfLongestSubstring('x'))
      console.log(lengthOfLongestSubstring('xxxsajsoiqwdjoiqwnxj'))
    }
  },
  created () {
    this.init()
  },
  mounted () {
    this.$el.innerHTML = this.$md(md)
  },
}

</script>

<style>
</style>
